<?php
require 'function.php';
global $vbulletin, $db, $vbphrase, $parentlist;

if ($vbulletin->options['danhmuctinhthanh_enabled'] && $parentlist[0] != 1)
{
    
  
        //Thanh pho da chon
        $forumid_history = SiteUtility::getCookie('bb_forumid_view');
        $numbNew = $vbulletin->options['number_display_city'];
        $idDefault = $vbulletin->options['city_id_default'];		
        $city = $vbulletin->input->clean_gpc('g', 'city', TYPE_STR);      
        if(isset($city) && $city != "0" && !empty($city)) {
                vbsetcookie('city', $city);                                     
        } else {
            $city = SiteUtility::getCookie('bb_city', $idDefault);
            
        }    
      

        //Danh sach thanh pho da chon                
        $listcity = SiteUtility::getCookie('bb_list_city');
        $time = time();
        vbsetcookie("list_city[$city]",$time."_".$city);
        
        if(isset($listcity) && $listcity != null)
            sort($listcity, 0);
        
        $count = count($listcity);   
        $listCity = array("all");  
        if($count > $numbNew) {
            for($i = $count-3; $i > $count - $numbNew; $i--){
                $aVal = explode('_', $listcity[$i]);
                if($aVal[1] != null && ! in_array($aVal[1], $listCity))
                    $listCity[] = $aVal[1];
            }
        } else { 
            for($i = $count-1; $i > 0; $i--){
                $aVal = explode('_', $listcity[$i]);
                if($aVal[1] != null && ! in_array($aVal[1], $listCity))
                    $listCity[] = $aVal[1];
            }
        }
        if(isset($city) && $city != "" && ! in_array($city, $listCity)){
            $listCity[] = $city;            
        }
        if(!in_array($idDefault, $listCity))
            $listCity[] = $idDefault;
                
      
      /* $db->query("INSERT INTO `forum4.2`.`site_city` (`city_id`, `name`, `name_short`, `desc`, `order`, `display`) 
        VALUES (NULL, 'Thái Bình', 'Thái Bình', '', '', '1')");
        $db->query("INSERT INTO `forum4.2`.`site_city` (`city_id`, `name`, `name_short`, `desc`, `order`, `display`) 
        VALUES (NULL, 'Thái Nguyên', 'Thái Nguyên', '', '', '1')");
        $db->query("INSERT INTO `forum4.2`.`site_city` (`city_id`, `name`, `name_short`, `desc`, `order`, `display`) 
        VALUES (NULL, 'Thanh Hóa', 'Thanh Hóa', '', '', '1')");
        $db->query("INSERT INTO `forum4.2`.`site_city` (`city_id`, `name`, `name_short`, `desc`, `order`, `display`) 
        VALUES (NULL, 'Thừa Thiên - Huế', 'Thừa Thiên - Huế', '', '', '1')");
        $db->query("INSERT INTO `forum4.2`.`site_city` (`city_id`, `name`, `name_short`, `desc`, `order`, `display`) 
        VALUES (NULL, 'Tiền Giang', 'Tiền Giang', '', '', '1')");
        $db->query("INSERT INTO `forum4.2`.`site_city` (`city_id`, `name`, `name_short`, `desc`, `order`, `display`) 
        VALUES (NULL, 'Trà Vinh', 'Trà Vinh', '', '', '1')");
        $db->query("INSERT INTO `forum4.2`.`site_city` (`city_id`, `name`, `name_short`, `desc`, `order`, `display`) 
        VALUES (NULL, 'Tuyên Quang', 'Tuyên Quang', '', '', '1')");
        $db->query("INSERT INTO `forum4.2`.`site_city` (`city_id`, `name`, `name_short`, `desc`, `order`, `display`) 
        VALUES (NULL, 'Vĩnh Long', 'Vĩnh Long', '', '', '1')");
        $db->query("INSERT INTO `forum4.2`.`site_city` (`city_id`, `name`, `name_short`, `desc`, `order`, `display`) 
        VALUES (NULL, 'Vĩnh Phúc', 'Vĩnh Phúc', '', '', '1')");
        $db->query("INSERT INTO `forum4.2`.`site_city` (`city_id`, `name`, `name_short`, `desc`, `order`, `display`) 
        VALUES (NULL, 'Yên Bái', 'Yên Bái', '', '', '1')");*/
		$items = $db->query_read("
			SELECT *
			FROM " . TABLE_PREFIX . "site_city 
			WHERE display = 1 ORDER BY `order` DESC, name");	
		
		$raovat_viewfilter_bit = "";
        $raovat_viewfilter_bit_popup = "";
        $raovat_first = "";
        $list_top = 1;
        $i = 1;
        $curUrl = SiteUtility::curPageURL();
       
		while($item = $db->fetch_array($items))
		{
			$item['name'] = $item['name'];
			$item['id'] = $item['city_id'];
			$item['short_name'] = $item['name'];
			$item['show_filterbit'] = true;
            $item['curUrl'] = $curUrl;			
			$templater = vB_Template::create('raovat_view_filters');		
            
            if($city == $item['id']){
               $templater->register('active', "active");                         
            }            
                                    
            if(in_array($item['id'], $listCity)){     
                $list_top = 1;
                $item['list_top'] = $list_top;
                $raovat_filter =& $item;
                $templater->register('raovat_filter', $raovat_filter);
                $raovat_first .= $templater->render();          
            } else {
                if($i > ($numbNew - count($listCity))){                    
                    $list_top = 0;
                    $item['list_top'] = $list_top;
                    $raovat_filter =& $item;
                    $templater->register('raovat_filter', $raovat_filter);
                    $raovat_viewfilter_bit_popup .=  $templater->render();
                } else {
                  
                    $list_top = 1;
                    $item['list_top'] = $list_top;
                    $raovat_filter =& $item;
                    $templater->register('raovat_filter', $raovat_filter);
                    $raovat_viewfilter_bit .= $templater->render();
               }
                $i++;
            }
           
          
		}
		$raovat_filter['show_filterbit'] = false;
        $raovat_filter['curUrl'] = $curUrl;
		$templater = vB_Template::create('raovat_view_filters');
        if($city == "all")
            $templater->register('all_active', "active");
		$templater->register('raovat_filter', $raovat_filter);
        $templater->register('forumid_history', $forumid_history);        
        $templater->register('raovat_viewfilter_bit_popup', $raovat_viewfilter_bit_popup);
		$templater->register('raovat_viewfilter_bit', $raovat_first.$raovat_viewfilter_bit);		
		$raovat_viewfilter = $templater->render();		
        $ad_location['raovat_viewfilter'] = $raovat_viewfilter;
}
   